﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Cocbook_DTO;
using Cocbook_DAL;

namespace Sale_and_manage
{
    public partial class frmAddCustomer : DevComponents.DotNetBar.Metro.MetroForm
    {
        public frmAddCustomer()
        {
            InitializeComponent();
        }

        private Validate Vali = new Validate();

        protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.F1:
                    {
                        btnAddCustomer_Click(null, null);
                        break;
                    }
                case Keys.F2:
                    {
                        btnClean_Click(null, null);
                        break;
                    }
            }
            return base.ProcessDialogKey(keyData);
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            this.Close();            
        }

        private void btnAddCustomer_Click(object sender, EventArgs e)
        {
            if (txtCustomerID.Text == "" || txtCustomerID.TextLength < 9)
            {
                highlighter2.SetHighlightColor(txtCustomerID, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                txtCustomerID.Focus();
                txtCustomerID.SelectAll();
            }
            else
            {
                if (txtCustomerName.Text == "")
                {
                    highlighter2.SetHighlightColor(txtCustomerName, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                    txtCustomerName.Focus();
                }
                else
                {
                    if (txtAddress.Text == "")
                    {
                        highlighter2.SetHighlightColor(txtAddress, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                        txtAddress.Focus();
                        ClearAll();
                    }
                    else
                    {
                        Customer c = new Customer(0, Convert.ToInt32(txtCustomerID.Text), txtCustomerName.Text, cboBirthday.Value, txtAddress.Text, txtEmail.Text, txtPhone.Text, txtMobiphone.Text);
                        CustomerDAL cDAL = new CustomerDAL();
                        cDAL.CreateCustomer(c);
                        ClearAll();
                    }
                }
            }
        }

        public void ClearAll()
        {
            txtAddress.Clear();
            txtEmail.Clear();
            txtCustomerID.Clear();
            txtCustomerName.Clear();
            txtMobiphone.Clear();
            txtPhone.Clear();
            highlighter2.SetHighlightColor(txtAddress,DevComponents.DotNetBar.Validator.eHighlightColor.None);
            highlighter2.SetHighlightColor(txtEmail,DevComponents.DotNetBar.Validator.eHighlightColor.None);
            highlighter2.SetHighlightColor(txtCustomerID,DevComponents.DotNetBar.Validator.eHighlightColor.None);
            highlighter2.SetHighlightColor(txtCustomerName,DevComponents.DotNetBar.Validator.eHighlightColor.None);
            highlighter2.SetHighlightColor(txtMobiphone,DevComponents.DotNetBar.Validator.eHighlightColor.None);
            highlighter2.SetHighlightColor(txtPhone, DevComponents.DotNetBar.Validator.eHighlightColor.None);
        }

        private void btnClean_Click(object sender, EventArgs e)
        {
            ClearAll();
        }

        private void txtCustomerID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtCustomerName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtPhone_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtMobiphone_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtCustomerID_Leave(object sender, EventArgs e)
        {
            if (txtCustomerID.TextLength < 9)
            {
                highlighter2.SetHighlightColor(txtCustomerID, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                lblValidate.Text = "Số CMND không hợp lệ";
            }
            else
            {
                CustomerDAL cDAL = new CustomerDAL();
                Customer c = new Customer();
                c = cDAL.GetCustomerByCMND(Convert.ToInt32(txtCustomerID.Text));
                if (c != null)
                {
                    highlighter2.SetHighlightColor(txtCustomerID, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                    lblValidate.Text = "Số CMND này đã đăng kí với tên "+c.CName;
                }
            }
        }

        private void txtCustomerID_TextChanged(object sender, EventArgs e)
        {
            highlighter2.SetHighlightColor(txtCustomerID, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }


        private void txtPhone_TextChanged(object sender, EventArgs e)
        {
            highlighter2.SetHighlightColor(txtPhone, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

        private void txtCustomerName_TextChanged(object sender, EventArgs e)
        {
            highlighter2.SetHighlightColor(txtCustomerName, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

        private void txtAddress_TextChanged(object sender, EventArgs e)
        {
            highlighter2.SetHighlightColor(txtAddress, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

        private void txtEmail_TextChanged(object sender, EventArgs e)
        {
            if (Vali.Email(txtEmail.Text))
            {
                highlighter2.SetHighlightColor(txtEmail, DevComponents.DotNetBar.Validator.eHighlightColor.None);
                lblValidate.Text = "";
            }
        }

        private void txtMobiphone_TextChanged(object sender, EventArgs e)
        {
            highlighter2.SetHighlightColor(txtMobiphone, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            lblValidate.Text = "";
        }

        private void txtEmail_Leave(object sender, EventArgs e)
        {
            if (txtEmail.Text !="" && !Vali.Email(txtEmail.Text))
            {
                txtEmail.Focus();
                txtEmail.SelectAll();
                highlighter2.SetHighlightColor(txtEmail, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                lblValidate.Text = "Email không hợp lệ";
            }
        }







        

        
    }
}